﻿using System;
using System.Data;
using System.Web.UI;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.ObjectResolutionHelper;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace CRS.Web.BaseData
{
    public partial class SelObjList : VcPage
    {
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private PageBuilder m_PageBuilder;

        private static string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataSet.xml"; }
        }

        private string ClassName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue(BaseDataKeyInfo.CLASS_NAME, this); }
        }

        private string TypeName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue(BaseDataKeyInfo.TYPE_NAME, this); }
        }
        private string KeyProperty
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue(BaseDataKeyInfo.KEY_PROPERTY, this); }
        }

        private string KeyContext
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue(BaseDataKeyInfo.KEY_CONTEXT,this); }
        }

        private string ConditionString
        {
            get
            {
                var obj = Session[Request.Url+ BaseDataKeyInfo.CONDITION_STRING];
                if (obj != null)
                    return obj.ToString();
                return m_CreateCtlHlp.ReturnQueryStringValue(BaseDataKeyInfo.CONDITION_STRING, this);
            }
        }

        private string OtherCtlContext
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue(BaseDataKeyInfo.OTHER_CTL_CONTEXT, this); }
        }




        public string QueryCondition
        {
            get
            {
                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }
        protected override void OnInit(EventArgs e)
        {
            Response.Expires = -1;
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this, SettingXmlPath);
            m_PageBuilder.SetPgListAtrrForSltPage(pgList, ClassName, TypeName);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            pgList.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
            if (!IsPostBack)
            {
                m_CreateCtlHlp.SetDdlCondition(pgList,ddlCondition);
                pgList.BindGrid();
            }
        }

        private object BindPgList()
        {
            DataSet ds;
            var acc = new BaseDataAccess();
            var sqlHelper = new SqlHelper();
            string sql = m_PageBuilder.GetPgListSqlValue(ClassName, TypeName);
            sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            sql += " " + QueryCondition;
            acc.BeginTransaction();
            try
            {
                string exSql = sql;
                if(ConditionString.Length > 0)
                {
                    exSql = "Select * from (" + sql + ") q Where " + ConditionString;
                }
                ds = sqlHelper.ExecuteDataSet(acc.Session, exSql);
            }
            finally
            {
                acc.CloseSession();
            }
            return ds;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
            pgList.BindGrid();
        }

        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
            pgList.BindGrid();
        }

        protected void pgList_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            string keyProName = m_PageBuilder.GetDialogKeyPropertyName(KeyProperty);
            string showProContext = KeyContext;
            string className = ClassName;
            string dataKeyValue = e.SelectedDataKeyValue;
            object obj = m_PageBuilder.GetObjectByHql(className, keyProName, dataKeyValue);

            string[] showProNames = showProContext.Split(',');
            for( int i=0;i<showProNames.Length;i++)
            {
                showProNames[i] = m_PageBuilder.GetDialogShowPropertyName(showProNames[i]);
            }
            if (obj != null)
            {
                string keyProValue = m_PageBuilder.GetPropertyValue(keyProName, obj).ToString();
                string[] reVal = new string[1 + showProNames.Length];
                reVal[0] = keyProValue;
                for(int i=0;i<showProNames.Length;i++)
                {
                    reVal[i + 1] = m_PageBuilder.GetPropertyValue(showProNames[i], obj).ToString();
                }
                Window.Close(reVal);
            }
            else
            {
                Window.Close(new[] {"", ""});
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Window.Close(new[] { "", "" });
        }
    }
}
